Page({
    data: {
        addressList: []
    },

    onLoad(options) {
        this.loadAddressList();
    },

    loadAddressList() {
        // 模拟地址数据
        const mockAddressList = [
            {
                id: 1,
                name: '张先生',
                phone: '13800138000',
                address: '北京市朝阳区建国路88号',
                isDefault: true
            },
            {
                id: 2,
                name: '李女士',
                phone: '13900139000',
                address: '北京市海淀区中关村大街1号',
                isDefault: false
            }
        ];
        this.setData({ addressList: mockAddressList });
    },

    selectAddress(e) {
        const id = e.currentTarget.dataset.id;
        const selectedAddress = this.data.addressList.find(item => item.id === id);

        // 返回订单页并带入选中的地址
        const pages = getCurrentPages();
        const prevPage = pages[pages.length - 2]; // 上一页(订单页)
        prevPage.setData({
            'address': selectedAddress
        });

        wx.navigateBack();
    },

    addAddress() {
        wx.navigateTo({
            url: '/pages/address/edit'
        });
    },

    editAddress(e) {
        e.stopPropagation(); // 阻止事件冒泡
        const id = e.currentTarget.dataset.id;
        wx.navigateTo({
            url: `/pages/address/edit?id=${id}`
        });
    },

    deleteAddress(e) {
        e.stopPropagation(); // 阻止事件冒泡
        const id = e.currentTarget.dataset.id;
        wx.showModal({
            title: '提示',
            content: '确定要删除这个地址吗？',
            success: (res) => {
                if (res.confirm) {
                    // 删除逻辑
                }
            }
        });
    },
});